<?php
	/*header("Expires: Mon, 26 Jul 2010 05:00:00 GMT" ); 
	header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 
	header("Cache-Control: no-cache, must-revalidate" ); 
	header("Pragma: no-cache" );
	header("Content-type: text/x-json");*/
	
	require_once("framework/config/config.main.php");
	require_once(FRAMEWORK_CLASSES_FILESYSTEM . "class.database.php");
	
	$method = isset($_REQUEST['method']) ? $_REQUEST['method'] : false;
	$data = array();
	
	if($method == 'getTasks') {
		$data = getTasks();
	} else {
		$data = array(
			'page' => 1,
			'total' => 1,
			'rows' => array(array("cell" => array(1, 'E', '--', 'Error', "--", "--", "The method provided was not available"))),
		);
	}
	exit(json_encode($data));
	
	function getTasks() {
		$pageno = isset($_POST['page']) ? $_POST['page'] : 1;
		$sortcol = isset($_POST['sortname']) ? $_POST['sortname'] : 'status';
		$sortdir = isset($_POST['sortorder']) ? $_POST['sortorder'] : 'DESC';
		$results = isset($_POST['rp']) ? $_POST['rp'] : 15;
		$condition_addition = false;
		
		if(isset($_POST['query']) && isset($_POST['qtype'])) {
			$condition_addition = " AND `".$_POST['qtype']."` LIKE '%".$_POST['query']."%' ";
		}
		
		$data = array(
			"page" => $pageno,
			"rows" => array(),
		);
		
		$database = new Database();
		$query = "SELECT * FROM `tasks_pending` WHERE `status` < 2";
		if($condition_addition !== false) $query .= $condition_addition;
		$query .= " ORDER BY `".$sortcol."` ".$sortdir." ";
		$query .= " LIMIT ".(($pageno - 1) * $results).", ".($pageno * $results);
		
		$sqldata = $database->fetch_array($query);
		if(is_array($sqldata) && count($sqldata) > 0) {
			$data['total'] = count($sqldata);
			foreach($sqldata as $row) {
				$completion = ($row['completion']*100) . '%';
				if($row['completion'] == 1) $completion = '<font color="green">'.$completion.'</font>';
				$data['rows'][] = array("cell" => array($row['id'], $row['status'], $completion, $row['title'], date("m/d/y", $row['duedate']), date("m/d/y", $row['startdate']), $row['notes']));
			}
		} else {
			$data['rows'][] = array("cell" => array(1, 'E', '--', 'Error', "--", "--", $sqldata));
		}
		return $data;
	}
?>